﻿Public Class AlarmCheckViewForm

    Private Sub AlarmCheckViewForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.AutoGenerateColumns = False
    End Sub


    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        QueryData()
    End Sub

    Private Sub QueryData()

        Dim strSql As String
        strSql = "Select * From tbccCheckAlarm where checkTime >=?p0 and checkTime<=?p1 and projectID= ?p2"
        Dim p(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p(0).ParameterName = "p0" : p(0).Value = UtilityClass.GetDateString(Me.dtpFrom.Value)
        p(1).ParameterName = "p1" : p(1).Value = UtilityClass.GetDateString(Me.dtpTo.Value)
        p(2).ParameterName = "p2" : p(2).Value = MainForm.pi.ProjectID

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, p)

        For Each r As DataRow In ds.Tables(0).Rows
            If r("resultLight") = 1 Then
                r("resultLight") = "正常"
            Else
                r("resultLight") = "异常"
            End If

            If r("resultPhone") = 1 Then
                r("resultPhone") = "正常"
            Else
                r("resultPhone") = "异常"
            End If

        Next

        Me.DataGridView1.DataSource = ds.Tables(0)


        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            For j As Integer = 1 To Me.DataGridView1.Columns.Count - 1
                If Convert.ToString(Me.DataGridView1.Rows(i).Cells(j).Value) = "异常" Then
                    Me.DataGridView1.Rows(i).Cells(j).Style.ForeColor = Color.Red
                End If
            Next
        Next



        If ds.Tables(0).Rows.Count = 0 Then
            msg.Show("指定的时间范围内没有自检记录")
        End If
    End Sub

    ''' <summary>
    ''' 检查时间范围是否合法
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValidate() As Boolean
        If Me.dtpTo.Value < Me.dtpFrom.Value Then
            msg.ShowWarning("起始时间不能大于终止时间。")
            Me.dtpFrom.Focus()
            Return False
        End If

        Return True
    End Function

End Class